package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.ActivityLeave;
import com.ruoyi.system.domain.ActivityUserSignature;
import com.ruoyi.system.domain.MeetingLeave;
import com.ruoyi.system.domain.MeetingUserSignature;
import com.ruoyi.system.mapper.ActivityLeaveMapper;
import com.ruoyi.system.mapper.ActivityUserSignatureMapper;
import com.ruoyi.system.service.IActivityLeaveService;
import com.ruoyi.system.service.IActivityUserSignatureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 活动请假审批Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-11
 */
@Service
public class ActivityLeaveServiceImpl implements IActivityLeaveService
{
    @Autowired
    private ActivityLeaveMapper activityLeaveMapper;
    @Autowired
    private IActivityUserSignatureService activityUserSignatureService;
    @Autowired
    private ActivityUserSignatureMapper activityUserSignatureMapper;

    /**
     * 查询活动请假审批
     *
     * @param id 活动请假审批主键
     * @return 活动请假审批
     */
    @Override
    public ActivityLeave selectActivityLeaveById(String id)
    {
        return activityLeaveMapper.selectActivityLeaveById(id);
    }

    /**
     * 查询活动请假审批列表
     *
     * @param activityLeave 活动请假审批
     * @return 活动请假审批
     */
    @Override
    public List<ActivityLeave> selectActivityLeaveList(ActivityLeave activityLeave)
    {
        return activityLeaveMapper.selectActivityLeaveList(activityLeave);
    }

    /**
     * 新增活动请假审批
     *
     * @param activityLeave 活动请假审批
     * @return 结果
     */
    @Override
    public int insertActivityLeave(ActivityLeave activityLeave)
    {
        String date = DateUtils.dateTimeNow();
        String id = date + IdUtils.fastSimpleEnglishUUID(5);
        activityLeave.setId(id);
        activityLeave.setCreateTime(DateUtils.getNowDate());
        return activityLeaveMapper.insertActivityLeave(activityLeave);
    }

    /**
     * 修改活动请假审批
     *
     * @param activityLeave 活动请假审批
     * @return 结果
     */
    @Override
    public int updateActivityLeave(ActivityLeave activityLeave)
    {
        activityLeave.setUpdateTime(DateUtils.getNowDate());
        int i = activityLeaveMapper.updateActivityLeave(activityLeave);
        ActivityLeave leave = activityLeaveMapper.selectActivityLeaveById(activityLeave.getId());
        if(!ObjectUtils.isEmpty(leave)){
            String status = activityLeave.getStatus();
            ActivityUserSignature activityUserSignature = new ActivityUserSignature();
            activityUserSignature.setUserId(leave.getUserId());
            activityUserSignature.setActivityId(leave.getActivityId());
            ActivityUserSignature signature = activityUserSignatureMapper.selectActivityUserSignature(activityUserSignature);
            if("1".equals( status)){
                //审批通过，修改签到信息为已请假
                signature.setApprovalNum(leave.getId());
                signature.setStatus("2");
                activityUserSignatureService.updateActivityUserSignature(signature);
            }else if("2".equals( status)){
                //（通过之后）审批撤销，修改签到信息为未签到
                signature.setStatus("0");
                activityUserSignatureService.updateActivityUserSignature(signature);
            }
        }
        return i;
    }


    /**
     * 删除活动请假审批信息
     *
     * @param id 活动请假审批主键
     * @return 结果
     */
    @Override
    public int deleteActivityLeaveById(String id)
    {
        return activityLeaveMapper.deleteActivityLeaveById(id);
    }
}
